package com.clever.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clever.bean.model.OnlineUser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

import com.clever.mapper.LogisticsMapper;
import com.clever.bean.shopping.Logistics;
import com.clever.service.LogisticsService;

import javax.annotation.Resource;

/**
 * 订单物流服务
 *
 * @Author xixi
 * @Date 2024-04-08 11:11:31
 */
@Service
public class LogisticsServiceImpl implements LogisticsService {

    private final static Logger log = LoggerFactory.getLogger(LogisticsServiceImpl.class);

    @Resource
    private LogisticsMapper logisticsMapper;

    /**
     * 分页查询订单物流列表
     *
     * @param pageNumber 页码
     * @param pageSize   每页记录数
     * @param orderId    订单id
     * @return Page<Logistics>
     */
    @Override
    public Page<Logistics> selectPage(Integer pageNumber, Integer pageSize, String orderId) {
        QueryWrapper<Logistics> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(orderId)) {
            queryWrapper.eq("order_id", orderId);
        }
        return logisticsMapper.selectPage(new Page<Logistics>(pageNumber, pageSize), queryWrapper);
    }

    /**
     * 根据订单物流id获取订单物流
     *
     * @param id 订单物流id
     * @return Logistics 订单物流信息
     */
    @Override
    public Logistics selectById(String id) {
        return logisticsMapper.selectById(id);
    }

    /**
     * 根据订单id获取列表
     *
     * @param orderId 订单id
     * @return List<Logistics> 订单物流列表
     */
    @Override
    public List<Logistics> selectListByOrderId(String orderId) {
        return logisticsMapper.selectList(new QueryWrapper<Logistics>().eq("order_id", orderId).orderByAsc("id"));
    }

    /**
     * 新建订单物流
     *
     * @param logistics  订单物流实体信息
     * @param onlineUser 当前登录用户
     * @return Logistics 新建后的订单物流信息
     */
    @Override
    public Logistics create(Logistics logistics, OnlineUser onlineUser) {
        logisticsMapper.insert(logistics);
        log.info("订单物流, 订单物流信息创建成功: userId={}, logisticsId={}", onlineUser.getId(), logistics.getId());
        return logistics;
    }

    /**
     * 修改订单物流
     *
     * @param logistics  订单物流实体信息
     * @param onlineUser 当前登录用户
     * @return Logistics 修改后的订单物流信息
     */
    @Override
    public Logistics update(Logistics logistics, OnlineUser onlineUser) {
        logisticsMapper.updateById(logistics);
        log.info("订单物流, 订单物流信息修改成功: userId={}, logisticsId={}", onlineUser.getId(), logistics.getId());
        return logistics;
    }

    /**
     * 保存订单物流
     *
     * @param logistics  订单物流实体信息
     * @param onlineUser 当前登录用户
     * @return Logistics 保存后的订单物流信息
     */
    @Override
    public Logistics save(Logistics logistics, OnlineUser onlineUser) {
        if (StringUtils.isNotBlank(logistics.getId())) {
            return create(logistics, onlineUser);
        }
        return update(logistics, onlineUser);
    }

    /**
     * 根据订单物流id删除订单物流信息
     *
     * @param id         订单物流id
     * @param onlineUser 当前登录用户
     */
    @Override
    public void delete(String id, OnlineUser onlineUser) {
        logisticsMapper.deleteById(id);
        log.info("订单物流, 订单物流信息删除成功: userId={}, logisticsId={}", onlineUser.getId(), id);
    }

    /**
     * 根据订单物流id列表删除订单物流信息
     *
     * @param ids        订单物流id列表
     * @param onlineUser 当前登录用户
     */
    @Override
    public void deleteBatchIds(List<String> ids, OnlineUser onlineUser) {
        logisticsMapper.deleteBatchIds(ids);
        log.info("订单物流, 订单物流信息批量删除成功: userId={}, count={}, logisticsIds={}", onlineUser.getId(), ids.size(), ids.toString());
    }

    /**
     * 根据订单id删除
     *
     * @param orderId    订单id
     * @param onlineUser 当前登录用户
     */
    @Override
    public void deleteByOrderId(String orderId, OnlineUser onlineUser) {
        logisticsMapper.delete(new QueryWrapper<Logistics>().eq("order_id", orderId));
        log.info("订单物流, 订单物流信息根据orderId删除成功: userId={}, orderId={}", onlineUser.getId(), orderId);
    }
}
